/*默认拦截器，你可以在这里统一处理请求参数、请求异常、业务异常等动作。*/
import {Inject, Injectable, Injector} from '@angular/core';
import {Router} from '@angular/router';
import {
    HttpErrorResponse,
    HttpHandler,
    HttpHeaderResponse,
    HttpInterceptor,
    HttpProgressEvent,
    HttpRequest,
    HttpResponse,
    HttpSentEvent,
    HttpUserEvent,
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {of} from 'rxjs/observable/of';
import {ErrorObservable} from 'rxjs/observable/ErrorObservable';
import {catchError, mergeMap} from 'rxjs/operators';
import {NzMessageService} from 'ng-zorro-antd';
import {_HttpClient} from '@delon/theme';
import {environment} from '@env/environment';
import {DA_SERVICE_TOKEN, ITokenService} from '@delon/auth';

/**
 * 默认HTTP拦截器，其注册细节见 `app.module.ts`
 */
@Injectable()
export class DefaultInterceptor implements HttpInterceptor {
    constructor(private injector: Injector,
        @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,) {}

    get msg(): NzMessageService {
        return this.injector.get(NzMessageService);
    }

    private goTo(url: string) {
        setTimeout(() => this.injector.get(Router).navigateByUrl(url));
    }

    private handleData(event: HttpResponse<any> | HttpErrorResponse): Observable<any> {
        // 可能会因为 `throw` 导出无法执行 `_HttpClient` 的 `end()` 操作
        this.injector.get(_HttpClient).end();

        //   if(this.tokenService.get().seniortoken===undefined){
        //          this.goTo('/passport/login');

        //   }
        console.log(event)
        // 业务处理：一些通用操作
        switch (event.status) {
            case 200:
            case 401: // 未登录状态码
                // this.goTo('/passport/login');
                break;
            case 403:
            case 404:
            case 500:

        }
        return of(event);
    }

    intercept(req: HttpRequest<any>, next: HttpHandler):
        Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {

        // 统一加上服务端前缀
        let url = req.url;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
            url = environment.SERVER_URL + url;
        }

        const newReq = req.clone({
            url: url
        });
        return next.handle(newReq).pipe(
                    mergeMap((event: any) => {
                        // 允许统一对请求错误处理，这是因为一个请求若是业务上错误的情况下其HTTP请求的状态是200的情况下需要
                        if (event instanceof HttpResponse && event.status === 200)
                            return this.handleData(event);
                        // 若一切都正常，则后续操作
                        return of(event);
                    }),
                    catchError((err: HttpErrorResponse) => this.handleData(err))
                );
    }
}
